//
//  DetailMovieViewController.m
//  豆瓣HW
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetailMovieViewController.h"
#import "DetailMovieView.h"
#import "LoginViewController.h"
#import "Movie.h"
#import "User.h"
#import "DataBaseHandle.h"

@interface DetailMovieViewController ()


@end

@implementation DetailMovieViewController


- (void)dealloc
{
    [_mov release];
//    [_detailMovie release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.mov.title;
    
//    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.automaticallyAdjustsScrollViewInsets = YES;
    
//    self.detailMovie = [[Movie alloc] init];
    
    
//    [self getData];
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
    
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(barAction:)];
    
    DetailMovieView *detailV = [[DetailMovieView alloc] initWithFrame:self.view.frame];
    
    detailV.labelDate.text = self.mov.release_date;
    detailV.labelTime.text = self.mov.runtime;
    detailV.labelStyle.text = self.mov.genres;
    detailV.labelCountry.text = self.mov.film_locations;
    detailV.labelMakerContent.text = self.mov.actors;
    detailV.labelPlotContent.text = self.mov.plot_simple;
    detailV.imageV.image = self.mov.image;
    detailV.labelRating.text = self.mov.rating;
    NSString *string1 = @"(";
    NSString *string2 = [string1 stringByAppendingString:self.mov.rating_count];
    NSString *string3 = [string2 stringByAppendingString:@"评论)"];
    detailV.labelRating_count.text = string3;
    
    [self.view addSubview:detailV];
    [detailV release];


}

//- (void)getData
//{
//
//    /*************************详情界面信息*********************/
//    NSString *string1 = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=";
//
//    NSString *detailConnector = [string1 stringByAppendingString: self.mov.movieId];
//    NSLog(@"%@", detailConnector);
//    
//    // detailConnector是对的,但是解析出来的东西是错的
//    
//    NSURL *detailUrl = [NSURL URLWithString:detailConnector];
//    
//    NSURLRequest *detailRequest = [NSURLRequest requestWithURL:detailUrl];
//    
//    NSURLSession *detailSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//    
//    NSURLSessionDataTask *getDetailTask = [detailSession dataTaskWithRequest:detailRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//        
//        NSDictionary *detailDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSMutableDictionary *detailTempDic = [detailDic objectForKey:@"result"];
//        NSLog(@"%@", detailTempDic);
//        // 这里很可能错
//        // 没获取到数据
//        self.detailMovie = [[Movie alloc] init];
//        [self.detailMovie setValuesForKeysWithDictionary:detailTempDic];
////        NSLog(@"%@", self.detailMovie);
//
////        self.detailMovie.actors = (NSString *)[detailTempDic objectForKey:@"actors"];
////            NSLog(@"%@", self.detailMovie.actors);
//        
//        DetailMovieView *detailV = [[DetailMovieView alloc] initWithFrame:self.view.frame];
//        
//        detailV.labelDate.text = self.detailMovie.release_date;
//            detailV.labelTime.text = self.detailMovie.runtime;
//            detailV.labelStyle.text = self.detailMovie.genres;
//            detailV.labelCountry.text = self.detailMovie.film_locations;
//        detailV.labelMakerContent.text = self.detailMovie.actors;
//        detailV.labelPlotContent.text = self.detailMovie.plot_simple;
//        detailV.imageV.image = self.detailMovie.image;
//        
//        
//        [self.view addSubview:detailV];
//        [detailV release];
//        
//    }];
//    
//    [getDetailTask resume];
//    
//    /*************************详情界面信息*********************/
//    
//    
//}

- (void)barAction:(UIBarButtonItem *)sender
{
    if (sender == self.navigationItem.leftBarButtonItem) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
        if (user.name == nil) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
            [loginVC release];
        } else {
            NSMutableArray *tempArr = [[DataBaseHandle shareDataBase] selectByMovieTitle:self.mov.title];
            if (![tempArr containsObject:user.name]) {
                [[DataBaseHandle shareDataBase] insertDataWithMovie:user MovieName:self.mov.title];
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alertC animated:YES completion:^{
                    
                    
                }];
                [alertC dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
                
            } else {
                //                NSLog(@"已收藏");
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已被收藏过" preferredStyle:UIAlertControllerStyleAlert];
                [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    
                    
                }]];
                [self presentViewController:alertC animated:YES completion:^{
                    
                    
                }];
                
            }
        }

    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
